package com.example.demo.config;

import com.example.demo.utils.SpringContextHolder;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.Environment;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;

/**
 * 获取全局配置
 *
 * @author AIWOBU
 * @date 2018.12.11
 */
public class GlobalConfig {

    private static Environment environment = SpringContextHolder.getBean(Environment.class);


    /**
     * 当前对象实例
     */
    private static GlobalConfig global = new GlobalConfig();

    /**
     * 保存全局属性值
     */
    private static Map<String, String> configMap = Maps.newHashMap();

    /**
     * 获取当前对象实例
     */
    public static GlobalConfig getInstance() {
        return global;
    }

    /**
     * 获取配置信息
     *
     * @param key key
     * @return return
     */
    public static String getConfig(String key) {
        Optional<String> value = Optional.ofNullable(configMap.get(key));
        if (value.isPresent()) {
            return value.get();
        } else {
            value = Optional.ofNullable(environment.getProperty(key));
            configMap.put(key, value.orElse(""));
            return value.orElse("");
        }
    }

    /**
     * oss的endpoint
     *
     * @param ossInfo
     * @return
     */
    public static String getOssEndpoint(String ossInfo) {
        return getConfig("ossInfo." + ossInfo + ".endpoint");
    }

    /**
     * oss的bucketName
     *
     * @param ossInfo
     * @return
     */
    public static String getOssBucketName(String ossInfo) {
        return getConfig("ossInfo." + ossInfo + ".bucketName");
    }

    /**
     * 获取项目的绝对路径
     *
     * @return
     */
    public static String getProjectPath() {
        return GlobalConfig.class.getClassLoader().getResource("").getPath();
    }

    /**
     * 获取工程路径
     *
     * @return
     */
    public static String getRequestLocalUrl() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String projectUrl = request.getScheme() + "://" + GlobalConfig.getConfig("final.path.localIp") + request.getContextPath() + "/";
        return projectUrl;
    }

    public static String getRequestFrontIpUrl() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String projectUrl = request.getScheme() + "://" + GlobalConfig.getConfig("final.path.frontIp") + request.getContextPath() + "/";
        return projectUrl;
    }

    /**
     * 获取默认配置oss信息
     *
     * @return return
     */
    public static String getDefOss() {
        String defOss = getConfig("ossInfo.default");
        return StringUtils.isNotEmpty(defOss) ? defOss : "";
    }

    /**
     * 获取oss信息
     *
     * @param ossInfo
     * @return
     */
    public static String getOssInfo(String ossInfo) {
        return StringUtils.isEmpty(ossInfo) ? GlobalConfig.getDefOss() : ossInfo;
    }



}
